<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/12/29
 * Time: 10:19
 */
namespace app\factory\controller;
use think\Db;
class User  extends Base
{
    /*
     * 用户列表
     */
    public function userList(){
        if(request()->isAjax ()){
            extract(input());
            $map['m_factory_id'] = session('f_uid');
            if(isset($key)&&$key!="")
            {
                $map['m_serial'] = ['like',"%" . $key . "%"];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['m_create_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['m_create_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['m_create_time'] = ['between time',[$start,$end]];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('factory_member')->where($map)->count();
            $lists = Db::name('factory_member')
                ->where($map)
                ->field(['m_id','m_serial','m_name','m_machine','m_create_time'])
                ->page($Nowpage,$limits)
                ->order('m_create_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch ('user/user_list');
    }

    /*
     * 注销账号
     */
    public function userOut(){
        Db::startTrans ();
        try{
            $m_machine = Db::name('factory_member')->where('m_id',input('id'))->value('m_machine');
            if($m_machine) {
                $this->Jpush1 (1 , (string)input('id') , '' , 4);
            }
            Db::name('factory_member')->delete (input('id'));
            Db::name('process')->where('p_uid',input('id'))->delete();
            Db::commit ();
            return json(['code'=>200,'msg'=>'注销成功']);
        }catch (\Exception $e){
            Db::rollback ();
            return json(['code'=>100,'msg'=>'注销失败']);
        }
    }


    /**
     * editPwd 修改管理员密码
     * @return \think\response\Json
     */
    public function editPwd(){
        extract(input());
        if(isset($type) && $type=='checkPassword'){
            $old_pwd = md5($old_pwd);
            $password =  Db::name('factory')->where("factory_id",session('f_uid'))->value("factory_password");
            if($password === $old_pwd){
                return json(['code' => 200, 'data' => '', 'msg' =>'true']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' =>'false']);
            }
        }else{
            $new_pwd = md5($new_pwd);
            $res =  Db::name('factory')->where("factory_id",session('f_uid'))->setField ('factory_password',$new_pwd);
            if($res){
                return json(['code'=>200,'msg'=>'密码修改成功，请重新登录！']);
            }else{
                return json(['code'=>100,'msg'=>'密码修改失败']);
            }
        }
    }
}